var lin = $.extend({}, lin);/* 全局对象 */


lin.msg = function (msg,type_icon){

        if(type_icon == 'error')
        {
            type_str = "错误";
        }
        else if(type_icon == "warning")
        {
            type_str = "警告";
        }
        else if(type_icon == "question")
        {
            type_str = "问题";
        }
        else
        {
            type_str = "提示";
            type_icon = "info";
        }

        $.messager.alert(type_str,msg,type_icon);
    }


lin.serializeObject = function(form){/*将form表单元素的值序列化成对象*/

    var o = {};
    $.each(form.serializeArray(),function(index){
        if(o[this['name']]){
            o[this['name']] = o[this['name']]+","+this['value'];
        }else{
            o[this['name']] = this['value'];
        }

    });

    return o ;

}


lin.obj2str = function (o) {
    var r = [];
    if (typeof o == "string") return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
    if (typeof o == "object") {
        if (!o.sort) {
            for (var i in o) r.push(i + ":" + lin.obj2str(o[i]));
            if ( !! document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
                r.push("toString:" + o.toString.toString());
            }
            r = "{" + r.join() + "}";
        } else {
            for (var i = 0; i < o.length; i++) r.push(lin.obj2str(o[i]));
            r = "[" + r.join() + "]";
        }
        return r;
    }
    return o.toString();
}

//格式化php时间戳
lin.getHis = function(ns)
{
    // 声明变量。
    var d, s;
    // 创建 Date 对象。
    d =  new Date(parseInt(ns)*1000);
    s = d.getFullYear() + "-";
    s += ("0"+(d.getMonth()+1)).slice(-2) + "-";
    s += ("0"+d.getDate()).slice(-2) + " ";
    s += ("0"+d.getHours()).slice(-2) + ":";
    s += ("0"+d.getMinutes()).slice(-2) + ":";
    s += ("0"+d.getSeconds()).slice(-2) ;
    return s;
}

/**
 * 增加命名空间功能
 *
 * 使用方法：sy.ns('jQuery.bbb.ccc','jQuery.eee.fff');
 */
lin.ns = function() {
    var o = {}, d;
    for ( var i = 0; i < arguments.length; i++) {
        d = arguments[i].split(".");
        o = window[d[0]] = window[d[0]] || {};
        for ( var k = 0; k < d.slice(1).length; k++) {
            o = o[d[k + 1]] = o[d[k + 1]] || {};
        }
    }
    return o;
};

lin.png = function() {
    var imgArr = document.getElementsByTagName("IMG");
    for ( var i = 0; i < imgArr.length; i++) {
        if (imgArr[i].src.toLowerCase().lastIndexOf(".png") != -1) {
            imgArr[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imgArr[i].src + "', sizingMethod='auto')";
            imgArr[i].src = "images/blank.gif";
        }
        if (imgArr[i].currentStyle.backgroundImage.lastIndexOf(".png") != -1) {
            var img = imgArr[i].currentStyle.backgroundImage.substring(5, imgArr[i].currentStyle.backgroundImage.length - 2);
            imgArr[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img + "', sizingMethod='crop')";
            imgArr[i].style.backgroundImage = "url('images/blank.gif')";
        }
    }
};
lin.bgPng = function(bgElements) {
    for ( var i = 0; i < bgElements.length; i++) {
        if (bgElements[i].currentStyle.backgroundImage.lastIndexOf(".png") != -1) {
            var img = bgElements[i].currentStyle.backgroundImage.substring(5, bgElements[i].currentStyle.backgroundImage.length - 2);
            bgElements[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img + "', sizingMethod='crop')";
            bgElements[i].style.backgroundImage = "url('images/blank.gif')";
        }
    }
};
lin.ns('lin.admin.info');





$.ajaxSetup({
    type : 'POST',
    error : function(XMLHttpRequest, textStatus, errorThrown) {/* 扩展AJAX出现错误的提示 */
        $.messager.progress('close');
        $.messager.alert('错误', XMLHttpRequest.responseText.split('<script')[0]);
    }
});




/*datagrid pacth*/
(function($){
    $.extend($.fn.datagrid.defaults.view, {
        insertRow: function(target, index, row){
            var state = $.data(target, 'datagrid');
            var opts = state.options;
            var dc = state.dc;
            var data = state.data;

            if (index == undefined || index == null) index = data.rows.length;
            if (index > data.rows.length) index = data.rows.length;

            for(var i=data.rows.length-1; i>=index; i--){
                var tr2 = opts.finder.getTr(target, i, 'body', 2);
                tr2.attr('datagrid-row-index', i+1);
                tr2.attr('id', state.rowIdPrefix + '-2-' + (i+1));
                var tr1 = opts.finder.getTr(target, i, 'body', 1);
                tr1.attr('datagrid-row-index', i+1);
                tr1.attr('id', state.rowIdPrefix + '-1-' + (i+1));
                if (opts.rownumbers){
                    tr1.find('div.datagrid-cell-rownumber').html(i+2);
                }
            }

            var fields1 = $(target).datagrid('getColumnFields', true);
            var fields2 = $(target).datagrid('getColumnFields', false);
            var rowId1 = state.rowIdPrefix + '-1-' + index;
            var rowId2 = state.rowIdPrefix + '-2-' + index;
            var tr1 = '<tr id="' + rowId1 + '" class="datagrid-row" datagrid-row-index="' + index + '">' + this.renderRow.call(this, target, fields1, true, index, row) + '</tr>';
            var tr2 = '<tr id="' + rowId2 + '" class="datagrid-row" datagrid-row-index="' + index + '">' + this.renderRow.call(this, target, fields2, false, index, row) + '</tr>';
            if (index >= data.rows.length){	// append new row
                if (data.rows.length){	// not empty
                    opts.finder.getTr(target, '', 'last', 1).after(tr1);
                    opts.finder.getTr(target, '', 'last', 2).after(tr2);
                } else {
                    dc.body1.html('<table cellspacing="0" cellpadding="0" border="0"><tbody>' + tr1 + '</tbody></table>');
                    dc.body2.html('<table cellspacing="0" cellpadding="0" border="0"><tbody>' + tr2 + '</tbody></table>');
                }
            } else {	// insert new row
                opts.finder.getTr(target, index+1, 'body', 1).before(tr1);
                opts.finder.getTr(target, index+1, 'body', 2).before(tr2);
            }

            data.total += 1;
            data.rows.splice(index, 0, row);

            this.refreshRow.call(this, target, index);
        },

        deleteRow: function(target, index){
            var state = $.data(target, 'datagrid');
            var opts = state.options;
            var data = state.data;

            opts.finder.getTr(target, index).remove();
            for(var i=index+1; i<data.rows.length; i++){
                var tr2 = opts.finder.getTr(target, i, 'body', 2);
                tr2.attr('datagrid-row-index', i-1);
                tr2.attr('id', state.rowIdPrefix + '-2-' + (i-1));
                var tr1 = opts.finder.getTr(target, i, 'body', 1);
                tr1.attr('id', state.rowIdPrefix + '-1-' + (i-1));
                tr1.attr('datagrid-row-index', i-1);
                if (opts.rownumbers){
                    tr1.find('div.datagrid-cell-rownumber').html(i);
                }
            }

            data.total -= 1;
            data.rows.splice(index,1);
        }
    });
})(jQuery);
